/*
 * This file is part of TradeHelper library.
 *
 * TradeHelper library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TradeHelper library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TradeHelper library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rag.core;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;

/**
 * User: mgarin Date: 04.04.12 Time: 16:06
 */

@XStreamAlias( "TopicType" )
public enum TopicType
{
    sell,
    pricecheck,
    buy,
    guild,
    closed,
    unknown;

    private static transient final List<String> closedTitles =
            Arrays.asList ( "закрыт", "закрыва", "продан", "продал", "ушло", "close", "не актуальн",
                    "неактуальн", "куплен", "удалит", "купил", "закрой", "zakryt" );

    private static transient final List<String> sellTitles =
            Arrays.asList ( "s>", "s >", "s<", "s <", "s ", "s->", "s>>", "s >>", "s:", "s:>",
                    "sell", "продам" );
    private static transient final List<String> buyTitles =
            Arrays.asList ( "b>", "b >", "b<", "b <", "b ", "b->", "b>>", "b >>", "b:", "b:>",
                    "buy", "куплю" );
    private static transient final List<String> tradeTitles =
            Arrays.asList ( "t>", "t >", "t<", "t <", "t ", "t->", "t>>", "t >>", "t:", "t:>",
                    "trade", "обмен" );
    private static transient final List<String> pricecheckTitles =
            Arrays.asList ( "pc", "pc>", "pc<", "pc <", "pc ", "p/c>", "ps>", "p/s>", "pc >",
                    "p/c >", "ps >", "ps:", "p/s >", "pc:", "p/c:", "p/s:" );

    private static transient final List<String> guildTitles = Arrays.asList ( "гильд", "guild" );
    private static transient final List<String> excludedGuildTitles = Arrays.asList ( "брунгильд" );

    public List<String> getTitles ()
    {
        if ( this.equals ( guild ) )
        {
            return guildTitles;
        }
        else if ( this.equals ( sell ) )
        {
            return sellTitles;
        }
        else if ( this.equals ( pricecheck ) )
        {
            return pricecheckTitles;
        }
        else if ( this.equals ( buy ) )
        {
            return buyTitles;
        }
        else
        {
            return closedTitles;
        }
    }

    public ImageIcon getIcon ()
    {
        return new ImageIcon ( TopicType.class.getResource ( "icons/topic/" + this + ".png" ) );
    }

    /**
     * Дополнительные методы
     */

    public static TopicType getTopicType ( ForumType forumType, String topic )
    {
        if ( checkType ( topic, closed, false ) )
        {
            return closed;
        }
        else if ( checkType ( topic, guild, true ) && isGuildType ( topic ) )
        {
            return guild;
        }
        else if ( checkType ( topic, sell, true ) )
        {
            return sell;
        }
        else if ( checkType ( topic, pricecheck, true ) )
        {
            return pricecheck;
        }
        else if ( checkType ( topic, buy, true ) )
        {
            return buy;
        }
        return forumType.equals ( ForumType.sell ) ? sell :
                ( forumType.equals ( ForumType.buy ) ? buy : unknown );
    }

    private static boolean checkType ( String topic, TopicType topicType, boolean startsOnly )
    {
        topic = topic.trim ().toLowerCase ();
        for ( String title : topicType.getTitles () )
        {
            if ( startsOnly ? topic.startsWith ( title ) : topic.contains ( title ) )
            {
                return true;
            }
        }
        return false;
    }

    private static boolean isGuildType ( String topic )
    {
        topic = topic.trim ().toLowerCase ();
        for ( String title : excludedGuildTitles )
        {
            if ( topic.contains ( title ) )
            {
                return false;
            }
        }
        return true;
    }
}